package com.example.rootwar.search;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

//node
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

}



public class SearchFunction {

    public static void main(String[] args) {

      int[] score={5,4,3,2,1};

        String[] relativeRanks = findRelativeRanks(score);


        System.out.println(Arrays.toString(relativeRanks));

    }



    public static String[] findRelativeRanks(int[] score) {

    String[] res=new String[score.length];


        List<Integer> list = new ArrayList<>();


        for (int i : score) {
            list.add(i);
        }


        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.intValue()-o2.intValue();
            }
        };
        list.sort(comparator);


        for (int i = 0; i < score.length; i++) {

            int oneValue = score[i];

            int zhebaninde=zhebaninde(oneValue,list);

            if (zhebaninde==list.size()-1){
                //first
                res[i]="Gold Medal";
            }else if (zhebaninde==list.size()-2){
                res[i]="Silver Medal";
            }else if (zhebaninde==list.size()-3){
                res[i]="Bronze Medal";
            }else {
                res[i]=String.valueOf(list.size()-zhebaninde);
            }

        }






        return res;

    }

    /**
     * 这帮查找
     * @param oneValue
     * @param list
     * @return
     */
    private static int zhebaninde(int oneValue, List<Integer> list) {
        int start=0;
        int end=list.size()-1;


        int mid=0;

        while (list.get(mid).intValue()!=oneValue){

            if (list.get(mid).intValue()>oneValue){
                end=mid-1;
            }else{
                start=mid+1;
            }
            mid=(start+end)/2;

        }

        return mid;


    }


    /**
     *
     * @param s
     * @return
     */
    public static int maxPower(String s) {
        if (s.length()==1){
            return 1;
        }

        int res=0;

        char[] chars = s.toCharArray();

        int start=0;
        int value=0;

        for (int i = 1; i < chars.length; i++) {
            if (chars[i]==chars[start]){
                //
                value++;
            }else {
                if ((value+1)>=res){
                    res=value+1;
                }
                start=i;
                value=0;
            }

        }
        return res;
    }
}
